---
title: 插件系统
icon: Album
---

```js
// ==UserScript==
// @name     测试插件
// @description 简单测试, IJKL移动摄像机
// @version  1.0.0
// @author   Littlefean
// ==/UserScript==

setInterval(() => {
  this.postMessage({
    type: "callAPIMethod",
    payload: {
      reqId: "getPressingKey",
      method: "getPressingKey",
      args: [],
    },
  });
  this.postMessage({
    type: "callAPIMethod",
    payload: {
      reqId: "getCameraLocation",
      method: "getCameraLocation",
      args: [],
    },
  });
});

let CAMERA_LOCATION = [0, 0];

/**
 * 在这里处理所有接函数调用接收到的结果
 */
this.onmessage = (e) => {
  const { payload, type } = e.data;
  if (type === "apiResponse") {
    if (payload.reqId === "getCameraLocation") {
      if (payload.error) {
      } else {
        CAMERA_LOCATION = payload.result;
      }
    }
    if (payload.reqId === "getPressingKey") {
      if (payload.error) {
      } else {
        const keyList = payload.result;
        if (keyList.includes("i")) {
          setCameraLocation(CAMERA_LOCATION[0], CAMERA_LOCATION[1] - 100);
        } else if (keyList.includes("k")) {
          setCameraLocation(CAMERA_LOCATION[0], CAMERA_LOCATION[1] + 100);
        } else if (keyList.includes("j")) {
          setCameraLocation(CAMERA_LOCATION[0] - 100, CAMERA_LOCATION[1]);
        } else if (keyList.includes("l")) {
          setCameraLocation(CAMERA_LOCATION[0] + 100, CAMERA_LOCATION[1]);
        }
      }
    }
  }
};

function setCameraLocation(x, y) {
  this.postMessage({
    type: "callAPIMethod",
    payload: {
      reqId: "setCameraLocation",
      method: "setCameraLocation",
      args: [x, y],
    },
  });
}
```
